# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :jwt_tool.py
# @Time      :2022/7/10 16:28
# @Author    :吕华龙
import time

import jwt
from flask import current_app


class Jwt_Tool():
    def create_token(self, payload, exp, secret=None):
        if not secret:
            secret = current_app.config['SECRET_KEY']

        payload['exp'] = int(time.time()) + exp

        token = jwt.encode(payload, key=secret, algorithm="HS256")

        return token

    def ver_token(self, token, secret=None):
        if not secret:
            secret = current_app.config["SECRET_KEY"]

        try:
            payload = jwt.decode(token, key=secret, algorithms="HS256")
            if payload['exp'] < int(time.time()):
                print("token已过期")
                payload = None
        except Exception as e:
            print(e)
            payload = None
        return payload
